<?php
/**
 * @description : user's information 
 * @author      : stephen.mo
 * @date        : 2010/09/02
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set('display_errors','on');

$current_page = OAuthConfig::PAGE_PLAN_INFORMATION;
$login_id     = (isset($_REQUEST['login_id'])) ? $_REQUEST['login_id'] : ((isset($_REQUEST['lid']) && !empty($_REQUEST['lid'])) ? base64_decode($_REQUEST['lid']) : NULL);
$call_back    = (isset($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : NULL;
$reback_url   = (isset($_REQUEST['reback_url'])) ? $_REQUEST['reback_url'] : NULL;
$access_token = (isset($_REQUEST['oauth_token'])) ? $_REQUEST['oauth_token'] : NULL;
$lang         = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
$pukey        = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
$pkg_id       = (isset($_REQUEST['pkg_id'])) ? $_REQUEST['pkg_id'] : NULL;
$pkg_version  = (isset($_REQUEST['pkg_v'])) ? $_REQUEST['pkg_v'] : NULL;
$pkg_price    = (isset($_REQUEST['pkg_price'])) ? $_REQUEST['pkg_price'] : NULL;
$login_id     = (!empty($login_id)) ? $login_id : OAuthUtils::getDefaultLoginId($login_id);

//setting vars
$params = array();
$params['oauth_token'] = $access_token;
$params['lang']        = $lang;
$params['login_id']    = $login_id;
$params['call_back']   = urlencode($call_back);
$params['reback_url']  = urlencode($reback_url);

//path setting.
$base_server_path      = OAuthConfig::OAuthDomainUrl($_SERVER);
$base_back_url         = OAuthConfig::OAuthGetBackUrl($_SERVER);
$reset_page_url        = OAuthConfig::OAuthGetRestUrl($_SERVER);
$oauth_token_str       = (!empty($access_token)) ? '&oauth_token='.$access_token : NULL;
$login_id_str          = (!empty($login_id)) ? '&login_id='.$login_id : NULL;
$quick_signin_url      = $base_server_path.OAuthConfig::DEFAULT_QUICKLY_LOGIN.'?reback_url='.urlencode($reset_page_url);
$message_url           = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;

//verify the oauth token
if (OAuthModelConfig::IS_NEED_CHECK_OAUTHTOKEN) {
    $ret = OAuthUtils::verifyAccessToken($access_token);
    if (!$ret || !$ret['flag']) {
        $other_msg = array('call_back'=>(isset($call_back) ? urlencode($call_back) : urlencode($reset_page_url)) );
        OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1005,1006),$other_msg);
    }
    //try to get login id from oauth token vars.
    if (empty($login_id)) {
        $user = unserialize($ret['data']['data']);
        $login_id = (!empty($user)) ? OAuthUtils::getLoginIdByEmail($user->user) : NULL;
    }
}

//check user is logined?
$is_logined     = FALSE;
if (OAuthUtils::chkIsLogin($login_id,$access_token)) {
    $login_id   = OAuthUtils::getDefaultLoginId($login_id);
    $is_logined = (!empty($login_id)) ? TRUE : FALSE;

}

//get ptoken & pukey & verify it .
$ptoken = OAuthUtils::getPToken();
if(!empty($pukey) && !empty($ptoken)) {
    $pukey = OAuthUtils::reGenPUKey($pukey,$ptoken);
}
else {
    OAuthUtils::delPToken();
    OAuthUtils::redirectTo('restart_url',$params);
}

//if login id is not empty then need to check the user can or not to buy a plan.
if (!empty($login_id)) {
    //check user can buy plan.
    $rev = OAuthUtils::chkIfUserCanBuyPlan($login_id);
    //if the user have a exists plan and this plan not expiry then show message to ask user ,his plan is still effective.
    if ((isset($rev) && $rev==1)) {
        $tmp_confirm_url = (isset($call_back)) ? $call_back : '#';
        $other_msg = array('login_id'=>$login_id,'call_back'=>urlencode($tmp_confirm_url));
        OAuthUtils::delPToken();
        OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_PROMPT,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1031),$other_msg);
        exit;
    }
}

//url setting.
$jqtouch_url      = $base_server_path.OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE;
$base_url         = '&reback_url='.urlencode($reback_url).$oauth_token_str.$login_id_str.'&lang='.$lang.'&pukey='.$pukey;
$back_url         = $base_server_path.OAuthConfig::DEFAULT_CHOOSE_PLAN_URL.'?call_back='.urlencode($call_back).$base_url;
$next_url         = $base_server_path.OAuthConfig::DEFAULT_CONFIRM_URL;
$ajax_url         = $base_server_path.OAuthConfig::DEFAULT_AJAX_URL;
$restart_url      = $base_server_path.OAuthConfig::DEFAULT_CMODEL_URL.'?call_back='.urlencode($call_back).$base_url;

//get package info.
if (!empty($pkg_id) && !empty($pkg_version)) {
    $pkgInfo = OAuthUtils::getPkgInfo($pkg_id,$pkg_version,$lang);
}
//check the package info
if (empty($pkgInfo) || empty($pkg_id) || empty($pkg_version)) {
    $tmp_goback_url = '?call_back='.urlencode($call_back).'&reback_url='.$reback_url.$oauth_token_str.$login_in_str.'&lang='.$lang.'&pukey='.$pukey;
    $retry_url = $base_server_path.OAuthConfig::DEFAULT_CHOOSE_PLAN_URL.$tmp_goback_url;
    $other_msg = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1013),$other_msg);
    exit;
}

//get form data from MC.
$fdata             = array();
$tmp_account_email = NULL;
$tmpData           = OAuthUtils::getFormData($pukey,$ptoken);

if ($tmpData->retFlag==1) $tmpData = $tmpData->data;
$last_page    = (isset($tmpData->current_page)) ? $tmpData->current_page : NULL;
$back_url_top = (isset($tmpData->m_step_back_II)&&!empty($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II.'&pukey='.$pukey : $base_back_url.'&pukey='.$pukey;

//check allow access pages.
$allow_pages = array(
    OAuthConfig::PAGE_PLAN_CHOOSE,
    OAuthConfig::PAGE_PLAN_CONFIRM,
);
if (!in_array($last_page,$allow_pages)) {
    OAuthUtils::delPToken();
    OAuthUtils::redirectTo('restart_url',$params);
}

//set data to memcache.
$fdata['m_login_id']      = (isset($tmpData->m_login_id)) ? $tmpData->m_login_id : $login_id;
$fdata['m_call_back']     = (isset($tmpData->m_call_back)) ? $tmpData->m_call_back : $call_back;
$fdata['m_lang']          = (isset($tmpData->m_lang)) ? $tmpData->m_lang : $lang;
$fdata['m_reback_url']    = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : $reback_url;
$fdata['m_model']         = (isset($tmpData->m_model)) ? $tmpData->m_model : 'TVB';
$fdata['m_step_back_I']   = (isset($tmpData->m_step_back_I)) ? $tmpData->m_step_back_I : '';
$fdata['m_step_back_II']  = (isset($tmpData->m_step_back_II)) ? $tmpData->m_step_back_II : '';
$fdata['m_step_back_III'] = $base_back_url;
$fdata['current_page']    = $current_page;
$fdata['p_plan_id']       = $pkg_id;
$fdata['p_version_id']    = $pkg_version;

$ret = OAuthUtils::setFormData($pukey,$ptoken,$fdata);
if (!$ret) {
    //not success need to retry it.
    $retry_url = OAuthConfig::OAuthGetBuyPlanFirst($params);
    $other_msg = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_PROMPT,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1000),$other_msg);
    exit;
}

//get tmp email and language vars.
$tmp_account_email = (isset($tmpData->p_bill_address)) ? $tmpData->p_bill_address : NULL;
$p_lang            = (isset($tmpData->p_lang)) ? $tmpData->p_lang : NULL;

//if user logined the get user's default setting.
if (empty($tmp_account_email) && !empty($login_id)) {
    $userInfo          = OAuthUtils::getUserInfoByLoginId($login_id);
    $p_lang            = (empty($p_lang)) ? $userInfo->result->lang_string : NULL;
    $tmp_account_email = $userInfo->result->email;
}

//set the language default checked
$p_lang           = (!empty($p_lang)) ? $p_lang : $lang; 
$checked_str      = OAuthModelConfig::setDefaultLang($p_lang);
//page info setting.
$user_info_page   = OPI::$user_information_page;
$language_info    = OPI::$language_info;
$payment_buttons  = OPI::$payment_button_info;
//$is_logined = false;

//setting the form datas into array.
$form = array();
$using_real_env                 = OAuthModelConfig::USING_TEMPLATE_ON_REAL_ENV;
$page                           = OAuthModelConfig::M_PAGE_OF_INFORMATION;
$current_titles                 = ($is_logined) ? $user_info_page[$lang]['user_info_title'] : $user_info_page[$lang]['user_login_title'];
$call_back_str                  = (isset($call_back) && !empty($call_back)) ? '&call_back='.urlencode($call_back) : NULL;
$reback_url_str                 = (isset($reback_url) && !empty($reback_url)) ? '&reback_url='.urlencode($reback_url) : NULL;
$lang_str                       = '&lang='.$lang;
$oauth_token_str                = (isset($access_token) && !empty($access_token)) ? '&oauth_token='.$access_token : NULL;
$cancel_url                     = $base_server_path.OAuthConfig::DEFAULT_AJAX_URL.'?item=logout'.$call_back_str.$reback_url_str.$lang_str.$oauth_token_str;

//if not logined will show quick login-in form.
if (!$is_logined) {
    $sign_in_page               = OPI::$login_page_info[$lang];
    $register_page              = OPI::$register_page_info[$lang];
    $forget_pass_page           = OPI::$forget_password[$lang];
    $user_info_page             = OPI::$user_information_page[$lang];
    $microtime                  = OAuthModelConfig::getMicroFloatTime(); 
    $forget_pwd_url             = $base_server_path.OAuthConfig::MEMBER_FORGET_URL.'?lang='.$lang.'&reback_url='.urlencode($reset_page_url); 
    $success_url                = $next_url;//$reset_page_url.'&pukey='.$pukey; 
    $form['lang']               = $lang;
    $form['public_note']        = $user_info_page['public_note'];
    $form['title']              = $sign_in_page['page_title'];
    $form['login_title']        = $sign_in_page['login_w_title'];
    $form['uinfo_title']        = $current_titles;//$sign_in_page['page_title'];
    //login div vars.
    $third_name                 = (isset($third_name) && !empty($third_name)) ? $third_name : 'h3g';
    $form['login_notes']        = $sign_in_page['qlogin_notes'][$third_name];
    $form['login_id']           = $sign_in_page['login_id'];
    $form['password']           = $sign_in_page['password'];
    //reg div vars.
    $form['reg_title']          = $register_page['reg_title'];
    $form['reg_email']          = array('title'=>$register_page['email'],'note'=>$register_page['email_msg']);
    $form['reg_pwd']            = array('title'=>$register_page['password'],'note'=>$register_page['password_msg']);
    $form['reg_repwd']          = array('title'=>$register_page['repassword'],'note'=>NULL);
    $form['reg_captcha']        = array('title'=>$register_page['captcha'],'note'=>$register_page['captcha_note']);
    $form['forget']             = array('title'=>$forget_pass_page['fpassword_title'],'link'=>$forget_pwd_url);
    //form vars.
    $form['action']             = array('login'=>$next_url,'reg'=>$next_url);
    $form['ajax_url']           = $ajax_url;
    $form['method']             = 'GET';
    $form['success_url']        = $success_url;
    $form['items']              = array('login'=>'tmpSignIn','reg'=>'tmpRegister');
    $form['microtime']          = $microtime; 
    $form['language']           = array('title'=>$language_info[$lang]['lang'],'value'=>NULL);
    $form['lang_info']          = $language_info[$lang];
    $form['checked']            = $checked_str;
    //hidden.
    $form['hidden']             = array(
                                    'call_back'=>$call_back,
                                    'reback_url'=>$reback_url,
                                    'lang'=>$lang,
                                    'pukey'=>$pukey,
                                    'pkg_id'=>$pkg_id,
                                    'pkg_v'=>$pkg_version,
                                    'is_submit'=>TRUE,
                                    'p_lang'=>$lang,
                                );
    //other hidden vars.
    if (!empty($access_token)) {
        $form['hidden']['oauth_token'] = $access_token;
    }
    if (!empty($login_id)) {
        $form['hidden']['login_id'] = $login_id;
    }
    
    //buttons vars.
    $form['msgbox_url']           = NULL;
    $form['restart_url']          = NULL;
    $form['button']['cancel']     = array('value'=>$payment_buttons[$lang]['cancel'],'link'=>$cancel_url);
    $form['button']['previous']   = array('value'=>$payment_buttons[$lang]['previous'],'link'=>$back_url);
    $form['button']['next']       = array('value'=>$payment_buttons[$lang]['next'],'link'=>NULL);
}
else {

    $form['lang']                 = $lang;
    $form['title']                = $user_info_page[$lang]['page_title'];
    $form['uinfo_title']          = $current_titles; 
    //if was logined.
    $form['method']               = 'GET';
    $form['action']               = $next_url;
    $form['ajax_url']             = $ajax_url;
    $form['msgbox_url']           = NULL;//$msgbox_url;
    $form['restart_url']          = $restart_url;
    $form['login_id']             = array('title'=>$user_info_page[$lang]['login_id'],'value'=>$tmp_account_email);
    $form['email']                = array('title'=>$user_info_page[$lang]['account_email'],'value'=>$tmp_account_email,'note'=>$user_info_page[$lang]['email_note']);
    $form['language']             = array('title'=>$language_info[$lang]['lang'],'value'=>NULL);
    $form['lang_info']            = $language_info[$lang];
    $form['checked']              = $checked_str;
    //hidden.
    $form['hidden']               = array(
                                        'call_back'=>$call_back,
                                        'reback_url'=>$reback_url,
                                        'lang'=>$lang,
                                        'pukey'=>$pukey,
                                        'pkg_id'=>$pkg_id,
                                        'pkg_v'=>$pkg_version,
                                        'is_submit'=>TRUE,
                                        'item'=>'userInfo',
                                        'p_lang'=>$lang,
                                    );
    //other hidden vars.
    if (!empty($access_token)) {
        $form['hidden']['oauth_token'] = $access_token;
    }
    if (!empty($login_id)) {
        $form['hidden']['login_id'] = $login_id;
    }

    //buttons.
    $form['button']['cancel']     = array('value'=>$payment_buttons[$lang]['cancel'],'link'=>$cancel_url);
    $form['button']['previous']   = array('value'=>$payment_buttons[$lang]['previous'],'link'=>$back_url);
    $form['button']['next']       = array('value'=>$payment_buttons[$lang]['next'],'link'=>NULL);
}

$form['a_step']               = 'two';
$form['login_status']         = $is_logined;
$form['is_logined']           = $is_logined;

//include thie template for plan cmodel page.
include "..".OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE.OAuthModelConfig::$mobile_teplate_page[$page];

?>
